package com.sync.syncmasterdata.customer.service.convert;


import com.sync.syncmasterdata.customer.pojo.CrmCustomer;
import com.sync.syncmasterdata.customer.pojo.OACustomer;
import com.sync.syncmasterdata.customer.service.impl.CPServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.sync.syncmasterdata.util.NormalUtil.*;

/**
 * crm 转 oa对象
 */
@Service
@Slf4j
public class CrmToOA {

    @Autowired
    private CPServiceImpl cpService;

    public OACustomer convertFun(CrmCustomer cus, String prod) {
        Map<String, JSONObject> priMap = cpService.getAreaMap(new JSONArray()).get("crm");

        String sheng = getMapValue(priMap, cus.getNew_province()) == null ? "" : getMapValue(priMap, cus.getNew_province()).get("new_area").toString();
        JSONObject cityMapValue=getMapValue(priMap, cus.getNew_city());
        log.info("------crm转oa中获取的map:" + cityMapValue);
        String shi = cityMapValue == null ? "" : cityMapValue.get("new_area").toString();
        Integer suozaics = cityMapValue==null ? null:stringToInt(cityMapValue.get("oaid").toString());
        Integer suozaisf = shengChange(sheng);

        OACustomer oac = new OACustomer();
        //客户名称
        oac.setKehumc(cus.getName());
        //最终客户
        oac.setZuizhongkehu(cus.getName());
        //客户类别
        oac.setKehulb(kehulbChange(cus.getNew_type()));
        oac.setQita("CRM推送");
        //u8客户编码
        oac.setU8kehubm(cus.getNew_num());
        oac.setShifouyzz(shifouyzzChange(cus.getNew_account()));
        //oa 城市id
        oac.setSuozaics(suozaics);
        oac.setSuozaisf(suozaisf);
        //市
        oac.setShi(shi);
        //省
        oac.setSheng(sheng);
        //客户状态
        oac.setKehuzt(cus.getStatecode() == 1 ? 1 : 0);
        oac.setId(prod.equals("edit") ? cus.getNew_oa_id() : null);
        //crm id
        oac.setCrmid(cus.getAccountid());
        return oac;

    }

    /**
     * 是否有资质转换
     *
     * @param _i
     * @return
     */
    private int shifouyzzChange(Integer _i) {
        if (Objects.isNull(_i)) {
            return 1;
        }
        switch (_i) {
            case 3:
                return 0;
            default:
                return 1;
        }
    }

    /**
     * crm -》 oa 的客户管理类型 转换
     *
     * @param _i
     * @return
     */
    private int kehulbChange(Integer _i) {
        if (Objects.isNull(_i)) {
            return 4;
        }
        switch (_i) {
            case 1:
                return 6;
            case 2:
                return 7;
            case 3:
                return 8;
            case 4:
                return 5;
            case 5:
                return 9;
            default:
                return 4;
        }
    }

    /**
     * crm 转 oa 省份字段 转换
     */
    private Integer shengChange(String _s) {
        Map<String, Integer> shengMap = new HashMap<>();
        shengMap.put("浙江省", 0);
        shengMap.put("湖南省", 1);
        shengMap.put("山东省", 2);
        shengMap.put("福建省", 3);
        shengMap.put("江苏省", 4);
        shengMap.put("安徽省", 5);
        shengMap.put("湖北省", 6);
        shengMap.put("山西省", 7);
        shengMap.put("北京市", 8);
        shengMap.put("贵州省", 9);
        shengMap.put("重庆市", 10);
        shengMap.put("四川省", 11);
        shengMap.put("上海市", 12);
        shengMap.put("海南省", 13);
        shengMap.put("陕西省", 14);
        shengMap.put("云南省", 15);
        shengMap.put("天津市", 16);
        shengMap.put("河北省", 17);
        shengMap.put("河南省", 18);
        shengMap.put("辽宁省", 19);
        shengMap.put("吉林省", 20);
        shengMap.put("黑龙江省", 21);
        shengMap.put("青海省", 22);
        shengMap.put("甘肃省", 23);
        shengMap.put("广西壮族自治区", 24);
        shengMap.put("广东省", 25);
        shengMap.put("江西省", 26);
        shengMap.put("江西省宁夏回族自治区", 27);
        shengMap.put("台湾省", 28);
        shengMap.put("新疆维吾尔自治区", 29);
        shengMap.put("内蒙古自治区", 30);
        if (shengMap.containsKey(_s)) {
            return shengMap.get(_s);
        } else {
            return null;
        }

    }
}
